package com.example.himalaya2.presenters;

import com.example.himalaya2.api.XimalayaApi;
import com.example.himalaya2.interfaces.IRecommendCallBack;
import com.example.himalaya2.interfaces.IRecommendPresenter;
import com.example.himalaya2.utils.Constants;
import com.example.himalaya2.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendPresenter implements IRecommendPresenter {

    private static final String TAG = "RecommendPresenter";

    private List<IRecommendCallBack> mCallBacks = new ArrayList<>();

    public static RecommendPresenter sInstance = null;
    private XimalayaApi mXimalayaApi;
    private List<Album> mCurrentRecommend = null;

    /*该类负责创建自己的对象，同时确保只有单个对象被创建*/
    /*获取单利对象*/
    public static RecommendPresenter getInstance() {  // 懒汉式单例
        if (sInstance == null) {
            synchronized (RecommendPresenter.class) {
                if (sInstance == null) {
                    sInstance = new RecommendPresenter();
                }
            }
        }
        //
        return sInstance;
    }
    private RecommendPresenter() {

    }

    @Override
    public void getRecommendList() {
        // 获取推荐内容
        // 封装参数
        // 接口： 3.10.6 获取猜你喜欢专辑
        updateLoading();
        mXimalayaApi = XimalayaApi.getInstance();
        mXimalayaApi.getRecommendList(new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                LogUtil.d(TAG,"thread name "+Thread.currentThread().getName());
                // 数据获取成功
                if (gussLikeAlbumList != null) {
                    // 更新UI
                    //upRecommendUI(gussLikeAlbumList.getAlbumList());
                    handlerRecommendResult(gussLikeAlbumList.getAlbumList());
                    LogUtil.d(TAG,"l of gussLikeAlbum"+gussLikeAlbumList.getAlbumList().size());
                }
            }

            @Override
            public void onError(int i, String s) {
                // 数据获取出错
                LogUtil.d(TAG,"error-- >" + i);
                LogUtil.d(TAG,"errorMsg-- >" + s);
                handleError();
            }
        });
    }

    public List<Album> getCurrentRecommend() {
        return this.mCurrentRecommend;
    }

    private void handleError() {
        // 通知ui
        if (mCallBacks !=null) {
            for (IRecommendCallBack callBack : mCallBacks) {
                callBack.onNetworkError();
            }
        }

    }

    @Override
    public void pullToRefresh() {

    }

    @Override
    public void loadMore() {

    }

    private void handlerRecommendResult(List<Album> albumList){
        // 通知页面更新
        if (albumList != null) {
            //  测试 推荐内容返回为空的情况
            //albumList.clear();
            if (albumList.size() == 0) {
                for (IRecommendCallBack callBack : mCallBacks) {
                    callBack.onEmpty();
                }
            } else {
                if (mCallBacks !=null) {
                    for (IRecommendCallBack callBack : mCallBacks) {
                        callBack.onRecommendListLoaded(albumList);
                    }
                    this.mCurrentRecommend = albumList;
                }
            }
        }
    }

    private void updateLoading() {
        if (mCallBacks !=null) {
            for (IRecommendCallBack callBack : mCallBacks) {
                callBack.onLoading();
            }
        }

    }

    @Override
    public void registerViewCallback(IRecommendCallBack callBack) {
        if (callBack != null && !mCallBacks.contains(callBack)) { // 防止重复加入
            mCallBacks.add(callBack);
        }

    }

    @Override
    public void unRegisterViewCallback(IRecommendCallBack callBack) {
        if (mCallBacks!=null) {
            mCallBacks.remove(callBack);
        }
    }
}
